package com.example.safehome;

import android.os.Bundle;
import android.widget.ListView;

import org.litepal.LitePal;
import org.litepal.crud.LitePalSupport;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

public class WaterInRecordActivity extends AppCompatActivity {
private List<WaterWarningRecord>recordList=new ArrayList<>();
private SwipeRefreshLayout swipeRefresh;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.water_activity_in_record);
        initrecord();
        WaterInAdapter adapter=new WaterInAdapter(WaterInRecordActivity.this,R.layout.water_inresource,recordList);
        ListView listView=(ListView)findViewById(R.id.lv_water_in_record);
        listView.setAdapter(adapter);
        Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }
    private void refreshRecord() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    Thread.sleep(2000);
                }catch (InterruptedException e) {
                    e.printStackTrace();;
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        initrecord();
                        WaterInAdapter adapter=new WaterInAdapter(WaterInRecordActivity.this,R.layout.water_inresource,recordList);
                        adapter.notifyDataSetChanged();
                        swipeRefresh.setRefreshing(false);
                    }
                });
            }
        }).start();
    }
    @Override
    protected void onStart() {
        super.onStart();
        initrecord();
        WaterInAdapter adapter=new WaterInAdapter(WaterInRecordActivity.this,R.layout.water_inresource,recordList);
        ListView listView=(ListView)findViewById(R.id.lv_water_in_record);
        listView.setAdapter(adapter);
    }
    @Override
    protected void onResume() {
        super.onResume();
        initrecord();
        WaterInAdapter adapter=new WaterInAdapter(WaterInRecordActivity.this,R.layout.water_inresource,recordList);
        ListView listView=(ListView)findViewById(R.id.lv_water_in_record);
        listView.setAdapter(adapter);
    }
    @Override
    protected void onRestart() {
        super.onRestart();
        initrecord();
        WaterInAdapter adapter=new WaterInAdapter(WaterInRecordActivity.this,R.layout.water_inresource,recordList);
        ListView listView=(ListView)findViewById(R.id.lv_water_in_record);
        listView.setAdapter(adapter);
    }
    private void initrecord() {
        List<WaterWarningRecord>records= LitePal.findAll(WaterWarningRecord.class);
        recordList.clear();
        for(WaterWarningRecord record:records) {
            recordList.add(record);
        }

    }
}
